/**
 * FileDeleteServlet.java
 * Allows deletion of existing media files in the blobstore.
 */
package com.example.myproject;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
   
/**
 * @author sejung
 * file delete class
 * 
 */
public class FileDeleteServlet extends HttpServlet {  

	private static final long serialVersionUID = 1L;

	public void doPost( HttpServletRequest req, HttpServletResponse resp)	
			throws IOException{
		
		Long id = Long.parseLong(req.getParameter("id"));
		Long parentid = Long.parseLong(req.getParameter("parentid"));
		
		
		UploadDAO upDao = new UploadDAO();
		UploadDTO master = null;
		
		//uploadDTO data retrieve
		master =  upDao.findById(parentid);

		if(master == null)resp.sendRedirect("/error") ;
		System.out.println("master========="+master);
		
		//delete master
		upDao.delete(master.getId());
		
		if (master.getContentType().contains("audio")) 
		{
			AudioUploadDAO audio = new AudioUploadDAO();
			System.out.println("audio========="+req.getParameter("audio"));
			audio.deleteByParentId(master.getId());
		
		}
		else if (master.getContentType().contains("video")) 
		{
			VideoUploadDAO video = new VideoUploadDAO();
			
			video.deleteByParentId(master.getId());

		}
		else if (master.getContentType().contains("image")) 
		{
			PictureUploadDAO picture = new PictureUploadDAO();
			picture.deleteByParentId(master.getId());
		}
		else if (master.getContentType().contains("pdf") || master.getContentType().contains("epub") )
		{
			BookUploadDAO book = new BookUploadDAO();
			book.deleteByParentId(master.getId());

		}
		
		resp.sendRedirect("/index.jsp");

	}
	
}